<?php

namespace app\admin\controller\mall;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="mall_goods")
 */
class Goods extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\mall\Goods();
        
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            // $data = [];
            // 提点规则描述 json 1-9提点30%；10-99999提点20%
            $row->goods_remind_describe = '';
            $row->goods_remind_json = serialize($post['goods_remind']);
            // print_r($data); exit;
            foreach ($post['goods_remind'] as $value) {
                if (empty($value[0]) && empty($value[1]) && empty($value[2])) {
                    $this->error('请填写商品提点百分比或提点范围袋数!');
                }
                // if ($value[0] <= 0) {
                //     $this->error('请填写正确商品提点百分比!');
                // }
                if ($value[1] >= $value[2]) {
                    $this->error('请填写正确商品提点范围袋数!');
                }
                $row->goods_remind_describe .= '商品范围(袋)'.$value[1].'~'.$value[2].' 提点'.$value[0].'(%); ';
            }
            $row->update_time = time();
            try {
                // $save = $row->save($data);
                $row->save();
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $this->success('保存成功');
            // $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        // $row->toArray();
        $row->goods_remind_list = unserialize($row->goods_remind_json);
        // print_r($row->goods_remind_list); exit;
        $this->assign('row', $row);
        return $this->fetch();
    }
}